<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务编号" prop="deviceId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务编号"
          class="home_input"
          clearable
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入样品名称"
          clearable
          class="home_input"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="deviceModel">
        <el-input
          v-model="queryParams.deviceModel"
          placeholder="请输入规格型号"
          clearable
          class="home_input"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造商" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入制造商"
          clearable
          class="home_input"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="出厂编号" prop="leavefactoryId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.leavefactoryId"-->
      <!--          placeholder="请输入出厂编号"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="测量范围" prop="measureRange">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.measureRange"-->
      <!--          placeholder="请输入测量范围"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="最大允许误差" prop="maxallowerror">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.maxallowerror"-->
      <!--          placeholder="请输入最大允许误差"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="检定状态" prop="calibrationstate">
        <!--        <el-input-->
        <!--          v-model="queryParams.calibrationstate"-->
        <!--          placeholder="请输入检定状态"-->
        <!--          clearable-->
        <!--          class="home_input"-->
        <!--        @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select v-model="queryParams.calibrationstate" placeholder="请选择检定状态" class="home_input">
          <el-option
            v-for="item in circulationList"
            :key="item.label"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="送检日期" prop="sddate">
        <el-date-picker clearable
                        v-model="queryParams.sddate"
                        type="date"
                        class="home_input"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择送检日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="送检单位" prop="calibrationcompany">
        <el-input
          v-model="queryParams.calibrationcompany"
          placeholder="请输入送检单位"
          clearable
          class="home_input"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="单位地址" prop="companyaddr">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.companyaddr"-->
      <!--          placeholder="请输入单位地址"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="检定日期" prop="calibrationdate">
        <el-date-picker clearable
                        v-model="queryParams.calibrationdate"
                        type="date"
                        class="home_input"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择检定日期">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="联系人员" prop="person">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.person"-->
      <!--          placeholder="请输入联系人员"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="联系电话" prop="iphone">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.iphone"-->
      <!--          placeholder="请输入联系电话"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="操作人员账号" prop="userId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.userId"-->
      <!--          placeholder="请输入操作人员账号"-->
      <!--          clearable-->
      <!--          class="home_input"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="下载日期" prop="cratetime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.cratetime"-->
      <!--          type="date"-->
      <!--                        class="home_input"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择下载日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="上传时间" prop="uploadtime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.uploadtime"-->
      <!--          type="date"-->
      <!--                        class="home_input"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择上传时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="检定结果" prop="calibresult">
        <el-input
          v-model="queryParams.calibresult"
          placeholder="请输入检定结果"
          clearable

          class="home_input"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="mini"
        @click="handleQuery">查询
      </el-button>
      <el-button size="mini"
      @click="resetQuery">重置
    </el-button>
  </el-form-item>
</el-form>

<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
  <el-button
    type="primary"
    plain
    icon="el-icon-plus"
    size="mini"
  @click="handleAdd"

  >新增
</el-button>
</el-col>
<el-col :span="1.5">
<el-button
  type="success"
  plain
  icon="el-icon-edit"
  size="mini"
  :disabled="single"
@click="handleUpdate"

>修改
</el-button>
  </el-col>
<!--<el-col :span="1.5">-->
<!--<el-button-->
<!--  type="danger"-->
<!--  plain-->
<!--  icon="el-icon-delete"-->
<!--  size="mini"-->
<!--  :disabled="multiple"-->
<!--@click="handleDelete"-->

<!--&gt;删除-->
<!--</el-button>-->
<!--  </el-col>-->
<!--<el-col :span="1.5">-->
<!--<el-button-->
<!--  v-if="scope.row.state == 3"-->
<!--  size="mini"-->
<!--  type="text"-->
<!--  icon="el-icon-download"-->
<!--@click="downloadList(scope.row)"-->

<!--&gt;下载-->
<!--</el-button>-->
<!--<el-button-->
<!--  type="warning"-->
<!--  plain-->
<!--  icon="el-icon-download"-->
<!--  size="mini"-->
<!--@click="handleExport"-->

<!--&gt;导出-->
<!--</el-button>-->
<!--  </el-col>-->
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  </el-row>

<el-table v-loading="loading" :data="waterleveltasksList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center"/>
<el-table-column label="任务编号" align="center" prop="taskId"/>
<el-table-column label="仪器检定编号" align="center" prop="deviceId"/>
<el-table-column label="样品名称" align="center" prop="deviceName"/>
<el-table-column label="规格型号" align="center" prop="deviceModel"/>
<el-table-column label="制造商" align="center" prop="manufacturer"/>
<el-table-column label="出厂编号" align="center" prop="leavefactoryId"/>
<el-table-column label="测量范围" align="center" prop="measureRange"/>
<el-table-column label="最大允许误差" align="center" prop="maxallowerror"/>
<el-table-column label="检定状态" align="center" prop="calibrationstate" :formatter="circulationStatusFilter"/>
<el-table-column label="送检日期" align="center" prop="sddate" width="180">
<template slot-scope="scope">
  <span>{{ parseTime(scope.row.sddate, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
<el-table-column label="送检单位" align="center" prop="calibrationcompany"/>
<el-table-column label="单位地址" align="center" prop="companyaddr"/>
<el-table-column label="检定日期" align="center" prop="calibrationdate" width="180">
<template slot-scope="scope">
  <span>{{ parseTime(scope.row.calibrationdate, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
  <!--      <el-table-column label="联系人员" align="center" prop="person" />-->
  <!--      <el-table-column label="联系电话" align="center" prop="iphone" />-->
  <!--      <el-table-column label="操作人员账号" align="center" prop="userId" />-->
  <!--      <el-table-column label="下载日期" align="center" prop="cratetime" width="180">-->
  <!--        <template slot-scope="scope">-->
  <!--          <span>{{ parseTime(scope.row.cratetime, '{y}-{m}-{d}') }}</span>-->
  <!--        </template>-->
  <!--      </el-table-column>-->
  <!--      <el-table-column label="上传时间" align="center" prop="uploadtime" width="180">-->
  <!--        <template slot-scope="scope">-->
  <!--          <span>{{ parseTime(scope.row.uploadtime, '{y}-{m}-{d}') }}</span>-->
  <!--        </template>-->
  <!--      </el-table-column>-->
<el-table-column label="仪器类别" align="center" prop="devicetype" :formatter='devicetypeFilter'/>
<el-table-column label="检定结果" align="center" prop="calibresult"/>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="120">
<template slot-scope="scope">
  <el-button
    size="mini"
    type="text"
    icon="el-icon-edit"
  @click="handleUpdate(scope.row)"

  >修改
</el-button>
<!--<el-button-->
<!--  size="mini"-->
<!--  type="text"-->
<!--  icon="el-icon-delete"-->
<!--@click="handleDelete(scope.row)"-->

<!--&gt;删除-->
<!--</el-button>-->
<el-button
  v-if="scope.row.calibrationstate == 3"
  size="mini"
  type="text"
  icon="el-icon-download"
@click="downloadList(scope.row)"

>下载
</el-button>
  </template>
  </el-table-column>
  </el-table>

<pagination
v-show="total>0"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
  @pagination="getList"
  />

  <!-- 添加或修改水位计检定任务报送对话框 -->
<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="150px">
  <el-row>
    <el-col :span="12">
      <el-form-item label="仪器检定编号" prop="deviceId">
        <el-input v-model="form.deviceId" placeholder="请输入仪器检定编号"/>
      </el-form-item>
      <el-form-item label="样品名称" prop="deviceName">
        <el-input v-model="form.deviceName" placeholder="请输入样品名称"/>
      </el-form-item>
      <el-form-item label="规格型号" prop="deviceModel">
        <el-input v-model="form.deviceModel" placeholder="请输入规格型号"/>
      </el-form-item>
      <el-form-item label="制造商" prop="manufacturer">
        <el-input v-model="form.manufacturer" placeholder="请输入制造商"/>
      </el-form-item>
      <el-form-item label="出厂编号" prop="leavefactoryId">
        <el-input v-model="form.leavefactoryId" placeholder="请输入出厂编号"/>
      </el-form-item>
      <el-form-item label="测量范围" prop="measureRange">
        <el-col :span="11">
          <el-input v-model="form.min" type="number" min="0" placeholder="最小值"/>
        </el-col>
        <el-col class="line" :span="2">~</el-col>
        <el-col :span="11">
          <el-input v-model="form.max" type="number" min="0" placeholder="最大值"/>
        </el-col>


        <!--        <el-input v-model="form.measureRange" placeholder="请输入测量范围"/>-->
      </el-form-item>
      <el-form-item label="联系电话" prop="iphone">
        <el-input v-model="form.iphone" placeholder="请输入联系电话"/>
      </el-form-item>

  </el-col>
  <el-col :span="12">
    <el-form-item label="最大允许误差" prop="maxallowerror">
      <el-input v-model="form.maxallowerror" placeholder="请输入最大允许误差"/>
    </el-form-item>
    <el-form-item label="送检日期" prop="sddate">
      <el-date-picker clearable
                      v-model="form.sddate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择送检日期">
      </el-date-picker>
    </el-form-item>
        <el-form-item label="送检单位" prop="calibrationcompany">
          <el-input v-model="form.calibrationcompany" placeholder="请输入送检单位"/>
        </el-form-item>

    <el-form-item label="单位地址" prop="companyaddr">
      <el-input v-model="form.companyaddr" placeholder="请输入单位地址"/>
    </el-form-item>
  <el-form-item label="拟检定日期" prop="calibrationdate">
    <el-date-picker clearable
                    v-model="form.calibrationdate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择检定日期">
    </el-date-picker>
  </el-form-item>
  <el-form-item label="联系人员" prop="person">
    <el-input v-model="form.person" placeholder="请输入联系人员"/>
  </el-form-item>

</el-col>
</el-row>


  </el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary"
@click="submitForm">确 定
</el-button>
<el-button
  @click="cancel">取 消
  </el-button>
  </div>
  </el-dialog>
  </div>
  </template>

<script>
import {
  listWaterleveltasks,
  getWaterleveltasks,
  delWaterleveltasks,
  addWaterleveltasks,
  updateWaterleveltasks
} from "@/api/system/waterleveltasks";
import {listSendcheckcompanydictionary} from "../../../api/system/sendcheckcompanydictionary";

export default {
  name: "Waterleveltasks",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水位计检定任务报送表格数据
      waterleveltasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      unitList: [],
      circulationList: [
        {
          label: '未检',
          value: "1",
        },
        {
          label: '已检',
          value: "2",
        },
        {
          label: '已上传',
          value: "3",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        deviceName: null,
        deviceModel: null,
        manufacturer: null,
        leavefactoryId: null,
        measureRange: null,
        maxallowerror: null,
        calibrationstate: null,
        sddate: null,
        calibrationcompany: null,
        companyaddr: null,
        calibrationdate: null,
        person: null,
        iphone: null,
        userId: null,
        cratetime: null,
        uploadtime: null,
        devicetype: null,
        calibresult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // taskid: [
        //   {required: true, message: "任务编号不能为空", trigger: "blur"}
        // ],
        iphone: [
          {required: true, message: "联系电话不能为空", trigger: "change"}
        ],
        person: [
          {required: true, message: "联系人员不能为空", trigger: "blur"}
        ],


        sddate: [
          {required: true, message: "送检时间不能为空", trigger: "blur"}
        ],
        calibrationcompany: [
          {required: true, message: "送检单位不能为空", trigger: "blur"}
        ],
        companyaddr: [
          {required: true, message: "单位地址不能为空", trigger: "blur"}
        ],
        maxallowerror: [
          {required: true, message: "最大误差不能为空", trigger: "blur"}
        ],
        deviceId: [
          {required: true, message: "仪器检定编号不能为空", trigger: "change"}
        ],
        deviceName: [
          {required: true, message: "样品名称不能为空", trigger: "blur"}
        ],
        deviceModel: [
          {required: true, message: "规格型号不能为空", trigger: "blur"}
        ],
        manufacturer: [
          {required: true, message: "制造商不能为空", trigger: "blur"}
        ],
        leavefactoryId: [
          {required: true, message: "出厂编号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUnit()
  },
  methods: {
    downloadList(row){
      const id = row.taskId || this.ids
      this.download(`system/waterleveltasks/down/${id}`, '', `水位计检测报告_${new Date().getTime()}.docx`)
    },
    /** 查询水位计检定任务报送列表 */
    getList() {
      this.loading = true;
      listWaterleveltasks(this.queryParams).then(response => {
        this.waterleveltasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        deviceId: null,
        deviceName: null,
        deviceModel: null,
        manufacturer: null,
        leavefactoryId: null,
        measureRange: null,
        maxallowerror: null,
        calibrationstate: null,
        sddate: null,
        calibrationcompany: null,
        companyaddr: null,
        calibrationdate: null,
        person: null,
        iphone: null,
        userId: null,
        cratetime: null,
        uploadtime: null,
        devicetype: null,
        calibresult: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getUnit()
      this.reset();
      this.open = true;
      this.title = "添加水位计检定任务报送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getUnit()
      this.reset();
      const taskId = row.taskId || this.ids
      getWaterleveltasks(taskId).then(response => {
        this.form = response.data;
        if (this.form.measureRange) {
          const str = this.form.measureRange.split("~")
          this.form.min = str[0]
          this.form.max = str[1]
        }
        this.open = true;
        this.title = "修改水位计检定任务报送";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskId != null) {
            updateWaterleveltasks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.measureRange = this.form.min + '~' + this.form.max
            addWaterleveltasks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    unitChange(row) {
      if (row) {
        this.unitList.forEach(item => {
          if (item.companyName == row) {
            this.form.companyaddr = item.companySite
          }

        })
      }
    },

    //  获取 送检单位
    getUnit() {
      listSendcheckcompanydictionary({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        if (res.code === 200) {
          this.unitList = res.rows
        }
        console.log(res, 'res')
      })
    },
    circulationStatusFilter(row) {
      switch (row.calibrationstate) {
        case '1':
          return '未检';
          break;
        case '2':
          return '已检';
          break;
        case '3':
          return '已上传';
          break;
      }
    },
    devicetypeFilter(row) {
      // 仪器类别：0  1  2  *
      switch (row.devicetype) {
        case 0:
          return '其他';
          break;
        case 1:
          return '雷达';
          break;
        case 2:
          return '超声';
          break;
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal.confirm('是否确认删除水位计检定任务报送编号为"' + taskIds + '"的数据项？').then(function () {
        return delWaterleveltasks(taskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/waterleveltasks/export', {
        ...this.queryParams
      }, `waterleveltasks_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss">
.home_input .el-input__inner {
  background-color: #F0F0F0;
}

.el-button--default {
  background-color: #F0F0F0;

&
:hover,

&
:active,

&
:focus {
  background-color: #F0F0F0;
  color: #606266;
  border: 1px solid #f0f0f0;
}

}

</style>
